//
//  BSSettingRootView.m
//  SuoSiApp
//
//  Created by zhang on 16/4/9.
//  Copyright © 2016年 Ym. All rights reserved.
//

#import "BSSettingRootView.h"

@interface BSSettingRootView()<UITextFieldDelegate>

@end

@implementation BSSettingRootView
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview];
    }
    return self;
}
- (void)addSubview{
    //    第一条状态栏
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, kScrenWidth, 20)];
    self.topView.backgroundColor = [UIColor colorWithRed:12.0/255 green:95.0/255 blue:35.0/255 alpha:1];
    [self addSubview:_topView];
    
    //    蓝牙信号
    self.bluetoothSignLB = [[UILabel alloc]initWithFrame:CGRectMake(kMartin20, 0, kMartin80 - 10, kMartin20)];
//    self.bluetoothSignLB.backgroundColor = BSRed;
    self.bluetoothSignLB.text = @"蓝牙信号";
    [self.topView addSubview:_bluetoothSignLB];
//    蓝牙信号的图片
    self.RSSIImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.bluetoothSignLB.right, self.bluetoothSignLB.top, kMartin30, self.bluetoothSignLB.height)];
    
    
    
    
    [self.topView addSubview:_RSSIImageView];
    
    
    
    //    电池电量
    self.batteryelectricLB = [[UILabel alloc]initWithFrame:CGRectMake(self.width - kMartin100 - kMartin50, 0, kMartin100 , kMartin20)];
//    self.batteryelectricLB.backgroundColor = BSRed;
    self.batteryelectricLB.text = @"电量剩余";
    [self.topView addSubview:_batteryelectricLB];
//    电池电量图片
    self.electImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.batteryelectricLB.right, self.batteryelectricLB.top, kMartin30, self.bluetoothSignLB.height)];
//    self.electImageView.backgroundColor = BSBlue;
    [self.topView addSubview:_electImageView];
    
    [self setRSSIImage];

    //    导航
    self.navigationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _topView.bottom, kScrenWidth, 63)];
    self.navigationView.userInteractionEnabled = YES;
    self.navigationView.image = [UIImage imageNamed:@"normal_title_bg"];
    [self addSubview:_navigationView];
    //    返回按钮
    self.backBT = BSButton;
    self.backBT.frame = CGRectMake(kMartin15, self.navigationView.height/3, self.navigationView.height/3 + 5, self.navigationView.height/3);
    [self.backBT setBackgroundImage:[UIImage imageNamed:@"normal_rebtn.png"] forState:(UIControlStateNormal)];
    [self.navigationView addSubview:_backBT];
    //    人物标识
    self.iconBT = BSButton;
    self.iconBT.frame = CGRectMake(self.backBT.right + kMartin5, self.backBT.top, self.backBT.width - 5, self.backBT.height);
    [self.iconBT setBackgroundImage:[UIImage imageNamed:@"normal_title_contactshead"] forState:(UIControlStateNormal)];
    [self.navigationView addSubview:_iconBT];
    //    用户
    self.userLB = [[UILabel alloc]initWithFrame:CGRectMake(self.iconBT.right + kMartin5, self.iconBT.top, kScrenWidth - kMartin10 - self.iconBT.right, self.iconBT.height)];
    

    NSMutableAttributedString *str;
    if ([BSUserSGton userHandle].realName && [BSUserSGton userHandle].realName.length > 1) {
        str =[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"欢迎您登陆, %@",[BSUserSGton userHandle].realName]];
    } else {
        str =[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"欢迎您登陆, %@",[BSUserSGton userHandle].user_loginName]];
    }
    
    [str addAttribute:NSForegroundColorAttributeName value:BSBTGreen range:NSMakeRange(str.length - 3,3)];
    
    self.userLB.attributedText = str;
    [self.navigationView addSubview:_userLB];
    
//    保存按钮
    self.saveBT = BSButton;
    self.saveBT.frame = CGRectMake(kMartin20, kScrenHeight - 78 - kMartin10, kScrenWidth - kMartin40, 40);
    
    self.saveBT.layer.cornerRadius = 5;
    self.saveBT.backgroundColor = BSSaveBTGreen;
//    [self.saveBT setBackgroundImage:[UIImage imageNamed:@"buttonbg"] forState:(UIControlStateNormal)];
    [self.saveBT setTitle:@"保存" forState:(UIControlStateNormal)];
    
    [self addSubview:_saveBT];
    
    //    scrollview显示中间内容
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navigationView.bottom, kScrenWidth, kScrenHeight - self.navigationView.bottom - (kScrenHeight - self.saveBT.top))];
//        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navigationView.bottom, kScrenWidth, self.saveBT.top - self.navigationView.bottom)];

//    self.scrollView.backgroundColor = BSGreen;
    [self addSubview:_scrollView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UILabel *)makeLableWithFrameY:(CGFloat)Y text:(NSString *)text{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 7,1)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(text.length - 4,4)];
    CGRect frame = CGRectMake(0, Y, kScrenWidth*3/8, 43);
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.attributedText = str;
    lable.backgroundColor = BSWhite;
    lable.font = [UIFont systemFontOfSize:20];
    [self.scrollView addSubview:lable];
    return lable;
}
// 必填项目
- (NSAttributedString *)getAttributeStringWithString:(NSString*)text{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(text.length - 1,1)];
    return str;
}

- (UILabel *)getLableWithFrameY:(CGFloat)Y text:(NSString *)text{
    
    CGRect frame = CGRectMake(0, Y, kScrenWidth*3/8 , 43);
    UILabel *lable = [[UILabel alloc]initWithFrame:frame];
    lable.text = text;
    lable.font = [UIFont systemFontOfSize:20];
    lable.backgroundColor = BSWhite;
    [self.scrollView addSubview:lable];
    return lable;
}


- (UITextField *)makeTextfeildWithFrameY:(CGFloat)YTF placeHolder:(NSString *)text{
    
    UITextField *textFeild = [[UITextField alloc]initWithFrame:CGRectMake(kScrenWidth*3/8, YTF, kScrenWidth *5/8, 43)];
    textFeild.delegate = self;;
    textFeild.placeholder = text;
    textFeild.backgroundColor = BSWhite;
    textFeild.textAlignment = NSTextAlignmentRight;
    textFeild.font = [UIFont systemFontOfSize:20];
    textFeild.borderStyle = UITextBorderStyleNone;
    [self.scrollView addSubview:textFeild];
    return textFeild;
}



- (void)setRSSIImage{
    
    NSLog(@"%ld",(long)[BSBluetoothSingleton shareBlutooth].RSSIValue);
    
    //    设置蓝牙图片
    if ([BSBluetoothSingleton shareBlutooth].RSSIValue > 50) {
        //        信号强
        _RSSIImageView.image = [UIImage imageNamed:@"signalHigh.png"];
        
    } else if ([BSBluetoothSingleton shareBlutooth].RSSIValue < 30 && [BSBluetoothSingleton shareBlutooth].RSSIValue > 1) {
        //        信号弱
        [[BSBluetoothSingleton shareBlutooth] warningForSianalWeak];
        _RSSIImageView.image = [UIImage imageNamed:@"signalLow.png"];
        
    }else if ([BSBluetoothSingleton shareBlutooth].RSSIValue < 50 && [BSBluetoothSingleton shareBlutooth].RSSIValue > 30){
        //    信号一般般
        _RSSIImageView.image = [UIImage imageNamed:@"signalMiddle.png"];
        
    }else{
        _RSSIImageView.image = [UIImage imageNamed:@"signalNo.png"];
        
    }
    
    //    设置电量
    NSLog(@"%f",[BSBluetoothSingleton shareBlutooth].batVolValue);
    
    //    设置电量图片
    if ([BSBluetoothSingleton shareBlutooth].batVolValue > 2.9) {
        //        电量强
        _electImageView.image = [UIImage imageNamed:@"electHigh.png"];
        
    } else if ([BSBluetoothSingleton shareBlutooth].batVolValue < 2.8 && [BSBluetoothSingleton shareBlutooth].batVolValue > 0) {
        //        电量弱
        _electImageView.image = [UIImage imageNamed:@"electLow.png"];
        [[BSBluetoothSingleton shareBlutooth]warningForElectricLow];
        
    }else if ([BSBluetoothSingleton shareBlutooth].batVolValue < 2.9 && [BSBluetoothSingleton shareBlutooth].batVolValue > 2.8){
        //    电量一般般
        _electImageView.image = [UIImage imageNamed:@"electMid.png"];
        
    }else{
        _electImageView.image = [UIImage imageNamed:@"common_blepower_0.png"];
    }
}

- (void)dealloc{


}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    if (self.IschangekeyBoard == YES) {
        CGFloat duration = 0.3;
        [UIView animateWithDuration:duration animations:^{
            
            self.transform = CGAffineTransformTranslate(self.transform, 0, self.keyboardOffset);
            self.IschangekeyBoard = NO;
            
        }];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
    
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
    CGPoint pp = [self convertPoint:CGPointMake(0, textField.bottom) toView:currentWindow];
    
    
    self.keyboardOffset = self.navigationView.bottom + pp.y + 44 + 253 - kScrenHeight;
    
    if (253 + pp.y + self.navigationView.bottom > kScrenHeight ) {
        NSLog(@"%f",self.keybordHeight);
        
        CGFloat duration = .3;
        
        [UIView animateWithDuration:duration animations:^{
            
            self.transform = CGAffineTransformTranslate(self.transform, 0, -self.keyboardOffset);
            self.IschangekeyBoard = YES;
            
        }];
    } ;
}


@end
